package com.zzyl.nursing.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.mapper.DeviceMapper;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.AlertDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-21
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{

    private static final int PROCESSED = 1;
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private IDeviceService iDeviceService;

    private Long getUserId() {
        return SecurityUtils.getUserId();
    }

    private String getUsername() {
        return SecurityUtils.getUsername();
    }

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertDataVo> selectAlertDataList(AlertData alertData)
    {
        //创建返回的对象列表
        List<AlertDataVo> alertDataVoArrayList;
        //查询alert_data表
        List<AlertData> alertDataList = alertDataMapper.selectAlertDataList(alertData);
        //查询device表将node_id字段填充到vo对象的nickname字段
        alertDataVoArrayList = alertDataList.stream().map(data -> BeanUtil.toBean(data, AlertDataVo.class)).collect(Collectors.toList());

        for (AlertDataVo alertDataVo : alertDataVoArrayList) {
            alertDataVo.setNickname(iDeviceService.lambdaQuery().eq(Device::getIotId, alertDataVo.getIotId())
                    .one().getNodeId());
        }
        return alertDataVoArrayList;
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

    /**
     * 处理报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int handleAlertData(AlertData alertData) {
        alertData.setStatus(PROCESSED);
        alertData.setProcessorId(getUserId());
        alertData.setProcessorName(getUsername());
        return updateById(alertData)?1:0;
    }

}
